package com.lzp.utilsexample

import android.Manifest
import android.os.Bundle
import android.text.TextUtils
import com.base.BaseActivity
import com.lzp.utilsexample.databinding.WifiDemeActivityBinding
import com.utils.DateTimeUtils
import com.utils.wifi.WifiConnectReceiver
import com.utils.wifi.WifiStateCallBack
import com.utils.wifi.WifiUtils
import com.utils.permission.PermissionUtils

/**
 * @describe:Heap
 * @Author: lixiaopeng
 * @Date: 2021/6/16
 */
class WifiDemoActivity : BaseActivity() {

    private val binding: WifiDemeActivityBinding by lazy { WifiDemeActivityBinding.inflate(layoutInflater) }

    private val wifiStateCallback: WifiStateCallBack =
            object : WifiStateCallBack {
                override fun onWifiDisable() {
                    binding.resultTv.text = "wifi 已关闭".withTime()
                }

                override fun onWifiDisabling() {
                    binding.resultTv.text = "wifi 正在关闭".withTime()
                }

                override fun onWifiEnable() {
                    binding.resultTv.text = "wifi 已打开".withTime()
                }

                override fun onWifiEnabling() {
                    binding.resultTv.text = "wifi 正在打开".withTime()
                }

                override fun onWifiUnknown() {
                    binding.resultTv.text = "wifi 未知".withTime()
                }

                override fun onConnected() {
                    binding.resultTv.text = "wifi 已连接".withTime()
                }
            }
    private val receiver: WifiConnectReceiver by lazy {
        WifiConnectReceiver()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
    }

    override fun setListener() {
        super.setListener()
        binding.isWifiEnable.setOnClickListener {
            binding.resultTv.text = if (WifiUtils.isWifiEnable(this)) {
                "Wifi 可用"
            } else {
                "Wifi 不可用"
            }
        }
        binding.connectWifi.setOnClickListener {
            if (!WifiUtils.isWifiEnable(this)) {
                WifiUtils.openWifi(this)
                return@setOnClickListener
            }

            if (!PermissionUtils.hasPermission(this, Manifest.permission.CHANGE_NETWORK_STATE)) {
                PermissionUtils.getPermission(this, arrayOf(Manifest.permission.CHANGE_NETWORK_STATE))
                return@setOnClickListener
            }
            val ssid = if (TextUtils.isEmpty(binding.wifiName.text.toString())){
                "Niu-Guest"
            }else{
                binding.wifiName.text.toString()
            }
            val pwd = if (TextUtils.isEmpty(binding.wifiPwd.text.toString())){
                "welcometoxiaoniu"
            }else{
                binding.wifiPwd.text.toString()
            }
            WifiUtils.connectWifi(this, ssid, pwd)
        }
        WifiUtils.registerWifiStateChangerReceiver(this, receiver, wifiStateCallback)
    }

    override fun clearListener() {
        super.clearListener()
        binding.isWifiEnable.setOnClickListener(null)
        binding.connectWifi.setOnClickListener(null)
        WifiUtils.unregisterWifiStateChangeReceiver(this, receiver)
    }

    fun String.withTime():String{
        return "${DateTimeUtils.long2time(System.currentTimeMillis(),DateTimeUtils.YMDHMS)}  $this"
    }
}